package com.gist.cithub.service.service.impl;

import com.gist.cithub.service.dao.CombineDao;
import com.gist.cithub.service.entity.CombineEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gist.common.utils.PageUtils;
import com.gist.common.utils.Query;

import com.gist.cithub.service.dao.InforDao;
import com.gist.cithub.service.entity.InforEntity;
import com.gist.cithub.service.service.InforService;


@Service("inforService")
public class InforServiceImpl extends ServiceImpl<InforDao, InforEntity> implements InforService {
    @Autowired
    InforDao inforDao;
    @Autowired
    CombineDao combineDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<InforEntity> page = this.page(
                new Query<InforEntity>().getPage(params),
                new QueryWrapper<InforEntity>()
        );
        return new PageUtils(page);
    }

    @Override
    public List<String> getServiceTypes() {
        QueryWrapper<InforEntity> queryWrapper = new QueryWrapper();
        queryWrapper.select("type").select("distinct type");
        List<InforEntity> list = inforDao.selectList(queryWrapper);
        List<String> res = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            res.add(list.get(i).getType());
        }
        return res;
    }

    @Override
    public List<InforEntity> searchByKeyword(String keyword) {
        QueryWrapper<InforEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.and(
                wrapper ->
                        wrapper.like("type", keyword).
                                or().
                                like("title", keyword).
                                or().
                                like("tags", keyword)
        );
        List<InforEntity> list = inforDao.selectList(queryWrapper);
        return list;
    }

    @Override
    public List<InforEntity> searchByType(String type) {
        QueryWrapper<InforEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.and(
                wrapper ->
                        wrapper.eq("type", type)
        );
        List<InforEntity> list = inforDao.selectList(queryWrapper);
        return list;
    }

    @Override
    public List<InforEntity> searchByTypeAndKeyword(String type, String keyword) {
        QueryWrapper<InforEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.and(
                wrapper ->
                        wrapper.eq("type", type)
        ).and(wrapper ->
                wrapper.like("title", keyword).
                        or().
                        like("tags", keyword));
        List<InforEntity> list = inforDao.selectList(queryWrapper);
        return list;
    }
    public CombineEntity getConductorUrl() {
        return combineDao.selectById(1);
    }


}